package com.example.onepeak.androiddemo.tab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

/**
 * Created by OnePeak on 2017/12/14.
 */

public class LevitateTabScrollView extends ScrollView {

    public LevitateTabScrollView(Context context) {
        this(context,null);
        init();
    }

    public LevitateTabScrollView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        init();
    }

    public LevitateTabScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        //增加视图监听 在整个视图树绘制完成后会回调
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                onScrollListener.onScroll(getScrollY());
            }
        });
    }
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onScrollListener != null) {
            onScrollListener.onScroll(t);
        }
    }

    private OnScrollListener onScrollListener;
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }
    public interface OnScrollListener{
        void onScroll(int scrollY);
    }
}